package com.rjpacket.supernews.app.models;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by aaa on 15-3-10.
 */
public class News implements Serializable {
    /**
     * private String content;// "",
     * private String thumbs;// ["d9410a1f0b135cade22b137926e0ffed&stype=6"],
     * private String db;// 1,
     * private String nmark;// 1,
     * private String time;// 1425949027642,
     * private String title;// "陕西纺织女工在日打工3年 赚回一套商品房钱",
     * private String stime;// 1425949027642,
     * private String source;// "腾讯新闻",
     * private String nid;// "7500820104430726050",
     * private String comment_num;// 334,
     * private String collect;// false,
     * private String itype;// 2
     */
    private String content;// "",
    private ArrayList<String> thumbs;// ["d9410a1f0b135cade22b137926e0ffed&stype=6"],
    private int db;// 1,
    private int nmark;// 1,
    private long time;// 1425949027642,
    private String title;// "陕西纺织女工在日打工3年 赚回一套商品房钱",
    private long stime;// 1425949027642,
    private String source;// "腾讯新闻",
    private String nid;// "7500820104430726050",
    private int comment_num;//   334,
    private boolean collect;// false,
    private int itype;// 2

    private String json;

    public String getJson() {
        return json;
    }

    public void setJson(String json) {
        this.json = json;
    }

    private Type type;

    private NewsDetail newsDetail;

    public NewsDetail getNewsDetail() {
        return newsDetail;
    }

    public void setNewsDetail(NewsDetail newsDetail) {
        this.newsDetail = newsDetail;
    }

    public Type getType() {
        return type;
    }

    public void setType(Type type) {
        this.type = type;
    }

    public enum Type {
        ZERO, ONE, THREE
    }

    private List<News> list;

    public List<News> getList() {
        return list;
    }

    public void setList(List<News> list) {
        this.list = list;
    }

    public void parseJSON(JSONObject jsonObject) {
        if (jsonObject != null) {
            list = new LinkedList<News>();
            try {
                JSONArray array = jsonObject.getJSONArray("news");
                int length = array.length();
                for (int i = 0; i < length; i++) {
                    try {
                        JSONObject json = array.getJSONObject(i);
                        News n = new News();
                        //以下是必须要获得的字段
                        n.setDb(json.optInt("db"));
                        n.setNmark(json.optInt("nmark"));
                        n.setTime(json.getLong("time"));
                        n.setStime(json.getLong("time"));
                        n.setTitle(json.getString("title"));
                        n.setSource(json.getString("source"));
                        n.setNid(json.optString("nid"));
                        n.setComment_num(json.getInt("comment_num"));
                        n.setCollect(json.getBoolean("collect"));
                        n.setItype(json.getInt("itype"));
                        //非必须的字段
                        n.setContent(json.optString("content"));
                        JSONArray jsonArray = json.optJSONArray("thumbs");
                        ArrayList<String> ithumbs = new ArrayList<String>();
                        for (int j = 0; j < jsonArray.length(); j++) {
                            String string = jsonArray.optString(j);
                            ithumbs.add(string);
                        }
                        n.setThumbs(ithumbs);

                        if (ithumbs.size() == 0) {
                            n.setType(Type.ZERO);
                        } else if (ithumbs.size() == 1) {
                            n.setType(Type.ONE);
                        } else {
                            n.setType(Type.THREE);
                        }

                        list.add(n);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    public boolean isCollect() {
        return collect;
    }

    public void setCollect(boolean collect) {
        this.collect = collect;
    }

    public int getComment_num() {
        return comment_num;
    }

    public void setComment_num(int comment_num) {
        this.comment_num = comment_num;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public int getDb() {
        return db;
    }

    public void setDb(int db) {
        this.db = db;
    }

    public int getItype() {
        return itype;
    }

    public void setItype(int itype) {
        this.itype = itype;
    }

    public String getNid() {
        return nid;
    }

    public void setNid(String nid) {
        this.nid = nid;
    }

    public int getNmark() {
        return nmark;
    }

    public void setNmark(int nmark) {
        this.nmark = nmark;
    }

    public String getSource() {
        return source;
    }

    public void setSource(String source) {
        this.source = source;
    }

    public long getStime() {
        return stime;
    }

    public void setStime(long stime) {
        this.stime = stime;
    }

    public ArrayList<String> getThumbs() {
        return thumbs;
    }

    public void setThumbs(ArrayList<String> thumbs) {
        this.thumbs = thumbs;
    }

    public long getTime() {
        return time;
    }

    public void setTime(long time) {
        this.time = time;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
}
